<?php
namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Spec extends Base
{
    use SoftDelete;

    //联查
    public function category(){
        return $this->belongsToMany("ProductCategory","SpecCategoryRelation","category_id","spec_id");
    }
    //建立唯一spec_id
    public function createSpecId($char="",$length=8){
        $count = 0;
        do {
            $spec_id = $char . date('Ymd');
            for ($i = 0; $i < $length; $i++) {
                $spec_id .= rand(0, 9);
            }
            $count = $this->getCount(['spec_id' => $spec_id]);   //保证生成的订单编号不会重复
        } while ($count>0);

        return $spec_id;
    }
    // 设置当前模型的数据库连接
    public function pName(){
        return $this->hasOne("SpecCategory","id","class_id")->bind(['class_name'=>"name"]);
    }


    // 创建分类树
    public function treeList($where)
    {


//        $list = cache('DB_TREE_SPEC');
//        if(!$list){

            $list = $this->where($where)->order('sort ASC,id ASC')->select();
            $treeClass = new \expand\Tree();
            $list = $treeClass->create($list);

//            cache('DB_TREE_SPEC', $list);
//        }
        return $list;

    }



}